import javax.naming.BinaryRefAddr;
import java.util.*;

public class Test {

}
class BinaryTree {

    public BTNode root;
    static class BTNode{
        BTNode left;
        BTNode right;
        char val;

        public BTNode(char val) {
            this.val = val;
        }
    }
    // 前序遍历
    //根左右
    //左根右
    //左右根
    void preOrder(BTNode root) {
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    void inOrder(BTNode root) {
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrder(BTNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    // 获取树中节点的个数
    public int size(BTNode root) {
        if(root==null) {
            return 0;
        }
        return size(root.right)+size(root.left)+1;

    }
    //遇到结点就+1
    public void size1(BTNode root) {
       int size= func(root,0);

    }
    public int func(BTNode root,int size) {
        if(root==null) {
            return 0;
        }
        size+= func(root.left,size);
        size+= func(root.right,size);
        return size+1;
    }
    // 获取叶子节点的个数
    public int getLeafNodeCount(BTNode root) {
        if(root==null) {
            return 0;
        }
        if(root.left==null&&root.right==null) {
            return 1;
        }
        return getLeafNodeCount(root.left)+getLeafNodeCount(root.right);
    }
        // 获取叶子节点的个数
    int size=0;
    public void getLeafNodeCount1(BTNode root) {
        if(root==null) {
            return ;
        }
        if(root.left==null&&root.right==null) {
            size++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    // 获取二叉树的高度
    int getHeight(BTNode root) {
        if(root==null) {
            return 0;
        }
        int a=getHeight(root.left);
        int b=getHeight(root.right);
        return 1+Math.max(a,b);
    }
    public BTNode createTree() {
        BTNode A=new BTNode('A');
        BTNode B=new BTNode('B');
        BTNode C=new BTNode('C');
        BTNode D=new BTNode('D');
        BTNode E=new BTNode('E');
        BTNode F=new BTNode('F');
        BTNode G=new BTNode('G');
        BTNode H=new BTNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;
        return A;
    }
    public static void main(String[] args) {
        BinaryTree binaryTree=new BinaryTree();
        binaryTree.root=binaryTree.createTree();
        System.out.println("============");
        binaryTree.preOrder(binaryTree.root);
        System.out.println();
        System.out.println(binaryTree.size(binaryTree.root));
        System.out.println(binaryTree.getLeafNodeCount2(binaryTree.root));
        System.out.println("-------------");
        System.out.println(binaryTree.getKLevelNodeCount3(binaryTree.root,4));
        System.out.println(binaryTree.getKLevelNodeCount3(binaryTree.root,3));
        System.out.println(binaryTree.getKLevelNodeCount3(binaryTree.root,2));
        System.out.println(binaryTree.getKLevelNodeCount3(binaryTree.root,1));
        System.out.println("=========");
        System.out.println(binaryTree.getHeight1(binaryTree.root));
        System.out.println(binaryTree.getHeight(binaryTree.root));
        System.out.println("=========");

        System.out.println(binaryTree.find(binaryTree.root,'h').val);
    }
    // 获取叶子节点的个数
    int getLeafNodeCount2(BTNode root) {
        if(root==null) {
            return 0;
        }
        if(root.left==null&&root.right==null) {
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }
    // 获取第K层节点的个数
    int getKLevelNodeCount3(BTNode root,int k){
        //获取第k-1层结点的左子节点+又子节点
        if(root==null) {
            return 0;
        }
        if(k==1) {
            return 1;
        }
        return getKLevelNodeCount3(root.left,k-1)+getKLevelNodeCount3(root.right,k-1);
    }
    // 获取二叉树的高度
    int getHeight1(BTNode root) {

        if(root==null) {
            return 0;
        }
        int left=getHeight1(root.left);
        int right=getHeight1(root.right);
        return Math.max(left, right)+1;
    }
    public BTNode find(BTNode root,char val) {
        if(root==null) {
            return null;
        }
        if(root.val==val) {
            return root;
        }
        BTNode btNode1= find(root.left,val);
        if (btNode1!=null) {
            return btNode1;
        }
        BTNode btNode2= find(root.right,val);
        if (btNode2!=null) {
            return btNode2;
        }
        return null;
    }
    public boolean isSameTree(BTNode p, BTNode q) {
        if(p!=null&&q!=null) {
            if(q.val == p.val) {
                return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
            }
        }else if(p==null&&q==null) {
            return true;
        }
        return false;
    }


    public void levelOrder(BTNode root) {
        List<List<Integer>> list=new LinkedList<>();
        if(root == null) {
            return;
        }
        Queue<BTNode> queue = new LinkedList<>();
        queue.offer(root);
        int size=0;
        while (!queue.isEmpty()) {
            List<Integer> p=new LinkedList<>();
            while (size!=0) {
                p.add((int) queue.poll().val);
                size--;
            }
            BTNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
}


class Solution {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
//    List<Integer> list=new ArrayList<>();
//
//    public List<Integer> preorderTraversal(TreeNode root) {
//        func(root);
//        return list;
//    }
//    public void func(TreeNode root) {
//        if(root==null) {
//            return;
//        }
//        list.add(root.val);
//        func(root.left);
//        func(root.right);
//    }

//    public List<Integer> preorderTraversal(TreeNode root) {
//        List<Integer> list1=new ArrayList<>();
//        if(root==null) {
//            return list1;
//        }
//        return func(root,list1);
//    }
//    public List<Integer> func(TreeNode root, List<Integer> list) {
//        List<Integer> list1=new ArrayList<>();
//        if(root==null) {
//            return null;
//        }
//        list1.add(root.val);
//        list.addAll(func(root.left,list1));
//        list.addAll(func(root.right,list1));
//
//        return list;
//    }
public List<Integer> preorderTraversal(TreeNode root) {
    List<Integer> list1=new ArrayList<>();
    if(root==null) {
        return list1;
    }
    return func(root,list1);
}
    public List<Integer> func(TreeNode root,List<Integer> list1) {
        if(root==null) {
            return null;
        }
        list1.add(root.val);
        list1=(func(root.left,list1));
        list1=(func(root.right,list1));
        return list1;
    }
}